﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Util;
using Xproject.Framework.Utility.Extensions;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.ExceptionHandling;
using Xproject.Framework.Core;
using System.Linq.Expressions;
using Model.Util.DTO;

namespace Repository.Util
{
    public static class CategoryAttrGroupRep
    {

        public static CategoryAttrGroups GetAttributeGroupById(this IRepositoryAsync<CategoryAttrGroups> repository, int AttrGroupId)
        {
            Expression<Func<CategoryAttrGroups, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.AttrGroupId == AttrGroupId);

            return repository.Query().Where(express).Select().FirstOrDefault();
        }


        public static List<CategoryAttrGroups> GetAttributeGroupListByCateId(this IRepositoryAsync<CategoryAttrGroups> repository, int categoryId,int? categoryType)
        {
            Expression<Func<CategoryAttrGroups, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.CategoryID == categoryId);
            if (categoryType.HasValue)
            {
                express = express.AndAlso(i => i.CategoryType == categoryType.Value);
            }
            
            return repository.Query().Where(express).Select().ToList();
        }
        public static List<CategoryAttr> GetAttributeListByAttrGroupId(this IRepositoryAsync<CategoryAttr> repository, int attrGroupId) {
            Expression<Func<CategoryAttr, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.AttrGroupId == attrGroupId);

            return repository.Query().Where(express).Select().ToList();
        }
        public static int GetAttrGroupIdByCateIdAndName(this IRepositoryAsync<CategoryAttrGroups> repository, int categoryId, string name)
        {
            Expression<Func<CategoryAttrGroups, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.CategoryID == categoryId);
            express = express.AndAlso(i => i.Name == name);
            CategoryAttrGroups cag = repository.Query().Where(express).Select().FirstOrDefault();
            if (cag != null)
            {
                return cag.AttrGroupId;
            }
            return 0;
        }


        public static int GetAttrIdByCateIdAndName(this IRepositoryAsync<CategoryAttr> repository,int categoryId,string name) {
            Expression<Func<CategoryAttr, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.CategoryID == categoryId);
            express = express.AndAlso(i => i.Name == name);
            CategoryAttr ca = repository.Query().Where(express).Select().FirstOrDefault();
            if (ca != null)
            {
                return ca.AttrId;
            }
            return 0;
         
        }

        public static CategoryAttr GetAttributeById(this IRepositoryAsync<CategoryAttr> repository, int attrId)
        {
            Expression<Func<CategoryAttr, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.AttrId == attrId);           
            return  repository.Query().Where(express).Select().FirstOrDefault();
           
        }

        public static List<CategoryAttr> GetAttributeListByCateId(this IRepositoryAsync<CategoryAttr> repository, int cateId)
        {
            Expression<Func<CategoryAttr, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.CategoryID == cateId);
            return repository.Query().Where(express).Select().ToList();
        }

        public static List<CategoryAttrModel> AdminGetAttributeList(this IRepositoryAsync<CategoryAttr> repository,int categoryId,string sort)
        {
            string sql = string.Format(@"SELECT [temp1].[AttrId],[temp1].[Name],[temp1].[CategoryID],[temp1].[AttrGroupId],[temp1].[ShowType],[temp1].[Isfilter],
                                        [temp1].DisplayOrder,
                                        [temp2].[name] AS [AttrGroupName] 
                                        FROM (SELECT * FROM [CategoryAttr] WHERE CategoryID={0})
                                         AS [temp1] 
                                         LEFT JOIN [CategoryAttrGroups] AS [temp2]
                                         ON [temp1].[AttrGroupId]=[temp2].[AttrGroupId] 
                                         ORDER BY [temp1].{1}",categoryId,sort);
            return repository.SqlQueryT<CategoryAttrModel>(sql).ToList();
        }

        public static List<CategoryAttrValues> GetAttributeValueListByAttrId(this IRepositoryAsync<CategoryAttrValues> repository, int attrId)
        {
            Expression<Func<CategoryAttrValues, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.AttrId == attrId);
            return repository.Query().Where(express).Select().OrderByDescending(i =>i.Isinput).ThenByDescending(i =>i.AttrValueDisplayOrder).ToList();
        }
        public static List<CategoryAttrValues> GetAttributeValueListByAttrId(this IRepositoryAsync<CategoryAttrValues> repository, List<int> attrIds)
        {
            Expression<Func<CategoryAttrValues, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => attrIds.Contains( i.AttrId));
            return repository.Query().Where(express).Select().OrderByDescending(i => i.Isinput).ThenByDescending(i => i.AttrValueDisplayOrder).ToList();
        }
        public static int  GetAttributeValueIdByAttrIdAndValue(this IRepositoryAsync<CategoryAttrValues> repository, int attrId, string attrValue)
        {
            Expression<Func<CategoryAttrValues, bool>> express = i => 1 == 1;
            express = express.AndAlso(i =>i.AttrId == attrId);
            express = express.AndAlso(i =>i.AttrValue == attrValue);
            CategoryAttrValues  temp= repository.Query().Where(express).Select().FirstOrDefault();
            if (temp != null)
            {
                return temp.AttrValueId;
            }
            return 0;
        }

        public static CategoryAttrValues GetAttributeValueById(this IRepositoryAsync<CategoryAttrValues> repository, int attrValueId)
        {
            Expression<Func<CategoryAttrValues, bool>> express = i => 1 == 1;
            express = express.AndAlso(i => i.AttrValueId == attrValueId);
            return repository.Query().Where(express).Select().FirstOrDefault();
        }
    }
}
